<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h1>登录</h1>
    <form id="loginForm">
        <input type="text" name="login_name" placeholder="Username" required />
        <input type="password" name="password" placeholder="Password" required />
        <button type="submit">登录</button>
    </form>
    <p>没有账号？去注册<a href="register.html">在这注册</a></p>

    <script>
        document.getElementById('loginForm').onsubmit = async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const data = Object.fromEntries(formData);
            const response = await fetch('/users/login', { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' } });
            const result = await response.json();
            if (result.success) {
                window.location.href = 'tasks.html?userId=' + result.userId;
            } else {
                alert(result.message);
            }
        };
    </script>
</body>
</html>